import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


/**
 * 
 * @author root
 * 
 */
public class GrayScale {

	/**
	 * Array of strings to be included in the NNET test
	 */
	public String[] nnet;
	/**
	 * pixel for a cell , max line in NNET test
	 */
	public final int X = 6, Y = 8, MAX_LINE = 16000;
	/**
	 * Thread writer
	 */
	public int count = 0;
	/**
	 * Number of cell to split the image
	 */
	private int blocchiY, blocchiX;
	/**
	 * Pointer of Main class to update the GUI
	 */
	private Main main;
	/**
	 * File test.txt to create
	 */
	private int fileToCreate = 0;
	/**
	 * FIle test.txt created
	 */
	private int fileCreated = 0;

	/**
	 * 
	 * @param path
	 *           path of the image selected
	 * @param main
	 *           pointer of Main class
	 */
	public GrayScale(String path, Main main) {
		this.main = main;

		// importo l'immagine

		try {
			BufferedImage rgbImage = ImageIO.read(new File(path));

			// converto l'immagine in scala di grigi
			BufferedImage greyImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
			greyImage.getGraphics().drawImage(rgbImage, 0, 0, null);

			// mostro l'immagine in bianco e nero
			main.changeIcon(new ImageIcon(greyImage.getScaledInstance(200, 180, Image.SCALE_AREA_AVERAGING)));

			// inserimento array: 100 = bianco, 0 = nero
			int[][] foto = new int[greyImage.getWidth()][greyImage.getHeight()];
			for (int j = 0; j < greyImage.getHeight(); j++) {
				for (int i = 0; i < greyImage.getWidth(); i++) {
					String hex = Integer.toHexString(greyImage.getRGB(i, j));
					String subhex = hex.substring(6);
					// converto da 0-255 a 0-100
					int conv = (Integer.parseInt(subhex, 16) * 100) / 255;
					foto[i][j] = conv;
				}
			}

			inputNnet(foto);

		} catch (Exception error) {
			JOptionPane.showMessageDialog(null, "Errore nell'inserimento della foto");
			main.changeIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
			error.printStackTrace();
		}

	}

	/**
	 * 
	 * @param immagine
	 *           array[][] con pixel ordinati
	 * @return restituisce array[] con formattazione per nnet
	 */
	public void inputNnet(int[][] immagine) {

		blocchiX = immagine.length / X;
		blocchiY = immagine[0].length / Y;
		nnet = new String[blocchiX * blocchiY];
		for (int i = 0; i < blocchiX * blocchiY; i++) {
			nnet[i] = "";
		}
		for (int matriceY = 0; matriceY < blocchiY; matriceY++) {

			new Thread(new Writer(GrayScale.this, immagine, matriceY, blocchiX)).start();
		}

	}

	/**
	 * Count++ when a thread Writer has finished
	 */
	public synchronized void add() {
		count++;
		if (count == blocchiY)
			start();
	}

	/**
	 * the array is completely written and you can write tests file
	 */
	public void start() {
		if (nnet.length < MAX_LINE) {
			new Thread(new MyFile(1, this, 0, nnet.length)).start();
			fileToCreate = 1;

		} else {
			fileToCreate = (nnet.length / MAX_LINE) + 1;
			for (int i = 1; i <= fileToCreate; i++) {
				if (i < fileToCreate)
					new Thread(new MyFile(i, this, MAX_LINE * (i - 1), (MAX_LINE * i) - 1)).start();
				else
					new Thread(new MyFile(i, this, MAX_LINE * (i - 1), nnet.length)).start();
			}

		}
	}

	/**
	 * fileCreated++ when a thread MyFile has finished
	 */
	public synchronized void done() {
		fileCreated++;
		if (fileCreated == fileToCreate) {
			JOptionPane.showMessageDialog(null, "Attendere nnet");
			new Nnet(blocchiX, blocchiY, fileCreated, main, blocchiX);
		}
	}

}
